package ejb.entities;

import java.io.Serializable;

import javax.persistence.*;

import org.apache.commons.codec.digest.DigestUtils;

import ejb.dto.UserDTO;

import java.util.Date;
import java.util.List;


/**
 * The persistent class for the users database table.
 * 
 */
@Entity
@Table(name="users")
@NamedQueries({
	@NamedQuery(name="User.findAll", query="SELECT u FROM User u"),
	@NamedQuery( name = "User.findById", query = "SELECT u FROM User u WHERE u.userid LIKE :id" )
})
public class User implements Serializable 
{
	private static final long serialVersionUID = 1L;
	
	//attributi utilizzati per le named query
	public static final String FIND_ALL = "User.findAll";
	public static final String FINDBYID = "User.findById";
	
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE)
	private String userid;

	private String address;

	@Temporal(TemporalType.DATE)
	private Date birth;

	private String city;

	private String country;

	private String name;

	private String password;

	private String surname;

	//bi-directional many-to-one association to Basicproduct
	@OneToMany(mappedBy="user")
	private List<Basicproduct> basicproducts;

	//bi-directional many-to-one association to Giftlist
	@OneToMany(mappedBy="user")
	private List<Giftlist> giftlists;

	//bi-directional many-to-many association to Group
	@ManyToMany(mappedBy="users")
	private List<Group> groups;

	//bi-directional many-to-one association to UserBasicproduct
	@OneToMany(mappedBy="user")
	private List<UserBasicproduct> userBasicproducts;

	//bi-directional many-to-one association to Pack
	@OneToMany(mappedBy="user")
	private List<Pack> packs;

	//bi-directional one-to-one association to UserPack
	@OneToOne(mappedBy="user")
	private UserPack userPack;

	public User() {
	}
	
	public User( UserDTO userDTO )
	{
		this.userid = userDTO.getEmail();
		this.address = userDTO.getAddress();
		this.birth = userDTO.getBirthDate();
		this.city = userDTO.getCity();
		this.country = userDTO.getCountry();
		this.name = userDTO.getFirstName();
		this.password = DigestUtils.sha256Hex( userDTO.getPassword() );
		this.surname = userDTO.getLastName();
	}

	public String getUserid() {
		return this.userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Date getBirth() {
		return this.birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	public String getCity() {
		return this.city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return this.country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSurname() {
		return this.surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public List<Basicproduct> getBasicproducts() {
		return this.basicproducts;
	}

	public void setBasicproducts(List<Basicproduct> basicproducts) {
		this.basicproducts = basicproducts;
	}

	public Basicproduct addBasicproduct(Basicproduct basicproduct) {
		getBasicproducts().add(basicproduct);
		basicproduct.setUser(this);

		return basicproduct;
	}

	public Basicproduct removeBasicproduct(Basicproduct basicproduct) {
		getBasicproducts().remove(basicproduct);
		basicproduct.setUser(null);

		return basicproduct;
	}

	public List<Giftlist> getGiftlists() {
		return this.giftlists;
	}

	public void setGiftlists(List<Giftlist> giftlists) {
		this.giftlists = giftlists;
	}

	/**
	 * associa all'utente la giftlist passata come parametro e associa alla giftlist passata come parametro l'utente
	 * @param giftlist
	 * @return
	 */
	public Giftlist addGiftlist(Giftlist giftlist) {
		getGiftlists().add(giftlist);
		giftlist.setUser(this);

		return giftlist;
	}

	/**
	 * rimuove la giftlist da utente e utente da giftlist
	 * @param giftlist
	 * @return
	 */
	public Giftlist removeGiftlist(Giftlist giftlist) {
		getGiftlists().remove(giftlist);
		giftlist.setUser(null);

		return giftlist;
	}

	public List<Group> getGroups() {
		return this.groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public List<UserBasicproduct> getUserBasicproducts() {
		return this.userBasicproducts;
	}

	public void setUserBasicproducts(List<UserBasicproduct> userBasicproducts) {
		this.userBasicproducts = userBasicproducts;
	}

	public UserBasicproduct addUserBasicproduct(UserBasicproduct userBasicproduct) {
		getUserBasicproducts().add(userBasicproduct);
		userBasicproduct.setUser(this);

		return userBasicproduct;
	}

	public UserBasicproduct removeUserBasicproduct(UserBasicproduct userBasicproduct) {
		getUserBasicproducts().remove(userBasicproduct);
		userBasicproduct.setUser(null);

		return userBasicproduct;
	}

	public List<Pack> getPacks() {
		return this.packs;
	}

	public void setPacks(List<Pack> packs) {
		this.packs = packs;
	}

	public Pack addPack(Pack pack) {
		getPacks().add(pack);
		pack.setUser(this);

		return pack;
	}

	public Pack removePack(Pack pack) {
		getPacks().remove(pack);
		pack.setUser(null);

		return pack;
	}

	public UserPack getUserPack() {
		return this.userPack;
	}

	public void setUserPack(UserPack userPack) {
		this.userPack = userPack;
	}

}